import { _decorator, AudioClip, Button, Component, Node, RichText, Sprite, tween, Vec3 } from 'cc';
import { Utils } from './tools/Utils';
import { SceneManager } from './tools/SceneManager';
import { Cons } from './tools/Cons';
import { L } from './tools/L';
import { EventAutoManager } from './tools/EventAutoManager';
import { AudioManager } from './tools/AudioManager';
import { ExitGame } from './tools/ExitGame';
const { ccclass, property } = _decorator;

@ccclass('GameOverScene')
export class GameOverScene extends Component {
    @property(RichText)
    historyHighestScoreText: RichText | null = null;
    @property(RichText)
    scoreText: RichText | null = null;
    @property(Button)
    restartButton: Button | null = null;
    @property(Button)
    exitGameButton: Button | null = null;
    private _eventAutoManager: EventAutoManager = new EventAutoManager();

    @property(AudioClip)
    buttonAudioClip: AudioClip | null = null;

    protected start(): void {
        L.info(`GameOverScene onLoad`);
        const data = SceneManager.inst.getSceneData(Cons.SCENES.GAME_OVER);
        L.info(`GameOverScene onLoad data: ${JSON.stringify(data)}`);

        if (data) {
            this.scoreText.string = `${data.score}`;
            this.historyHighestScoreText.string = `${data.historyHighestScore}`;
        }

        AudioManager.inst.pause();
        this._eventAutoManager.on(this.restartButton.node, Button.EventType.CLICK, () => {
            AudioManager.inst.playOneShot(this.buttonAudioClip);
            SceneManager.inst.loadScene(Cons.SCENES.GAME, {}, () => { })
        }, this);
        
        this._eventAutoManager.on(this.exitGameButton.node, Button.EventType.CLICK, () => {
            AudioManager.inst.playOneShot(this.buttonAudioClip);
            ExitGame.inst.exitGame();
        }, this);
    }

    protected onDestroy(): void {
        this._eventAutoManager.offAll();
    }
}

